package luminus.origin.commands.rewrite

import luminus.origin.utils.send
import org.bukkit.Bukkit
import org.bukkit.World
import org.bukkit.entity.Player
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.command.*
import taboolib.module.lang.sendLang


@CommandHeader("time", permission = "luminus.time", permissionDefault = PermissionDefault.OP)
object TimeSetter {
    @Awake(LifeCycle.ENABLE)
    fun initSimpleCommands() {
        simpleCommand("day", permission = "luminus.time.day") { sender, _ ->
            sender.setTime(1000)
        }
        simpleCommand("night", permission = "luminus.time.night") { sender, _ ->
            sender.setTime(13000)
        }
        simpleCommand("midnight", permission = "luminus.time.midnight") { sender, _ ->
            sender.setTime(18000)
        }
        simpleCommand("noon", permission = "luminus.time.noon") { sender, _ ->
            sender.setTime(6000)
        }
        simpleCommand("sunrise", permission = "luminus.time.sunrise") { sender, _ ->
            sender.setTime(23000)
        }
    }
    
    private fun ProxyCommandSender.setTime(time: Long): World {
        val player = this.castSafely<Player>()
        lateinit var world: World
        if (player == null) {
            world = Bukkit.getWorld("world")!!
            world.time = time
        } else {
            world = player.world
            world.time = time
        }
        sendLang("commands-time-set", world.name, time)
        return world
    }

    @CommandBody(permission = "luminus.time.set")
    val set = subCommand {
        dynamic("time name") {
            execute<ProxyCommandSender> { sender, context, _ ->
                when (context["time name"]) {
                    "day" -> {
                        sender.setTime(1000)
                    }
                    "night" -> {
                        sender.setTime(13000)
                    }
                    "midnight" -> {
                        sender.setTime(18000)
                    }
                    "noon" -> {
                        sender.setTime(6000)
                    }
                    "sunrise" -> {
                        sender.setTime(23000)
                    }
                }
            }
        }
        int("time tick") {
            execute<ProxyCommandSender> { sender, context, _ ->
                sender.setTime(context.int("time tick").toLong())
            }
        }
    }

    @CommandBody(permission = "luminus.time.get")
    val get = subCommand {
        execute<ProxyCommandSender> { sender, _, _ ->
            val player = sender.castSafely<Player>()
            if (player == null) {
                val world = Bukkit.getWorld("world")
                world?.name?.let { world.time.let { it1 -> sender.sendLang("command-time-get", it, it1) } }
            } else {
                sender.sendLang("commands-time-get", player.world.name, player.world.time)
            }
        }
    }

}